package Control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.UserDao;
import Model.User;

/**
 * Servlet implementation class RegisterController
 */
public class RegisterController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=request.getParameter("username");
		System.out.println("usern---"+name);
		String pwd1=request.getParameter("pwd1");
		String pwd2=request.getParameter("pwd2");
		UserDao ud=new UserDao();
		User u=new User();
		List<User> ls=ud.getByName(name);
		String responseMessage="";
		String path="Register.jsp";
		if(ls.size()>0){
			responseMessage="用户名已存在";
			path="Login.jsp";
		}else if(pwd1.equals(pwd2)){
			u.setU_name(name);
			u.setU_pwd(pwd1);
			u.setU_pwd(pwd2);
			ud.addUser(u);
			path="Login.jsp";
		}else{
			responseMessage="两次密码输入不一致，请重新输入";
			path="Register.jsp";
		}
		request.getRequestDispatcher(path).forward(request, response);
	}

		
		
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
